// Copyright (c) 2022 CHANGLEI. All rights reserved.

import 'package:aircraft_war/bloc/bloc_listenable.dart';
import 'package:aircraft_war/bloc/game_score_cubit.dart';
import 'package:aircraft_war/bloc/game_state_cubit.dart';
import 'package:aircraft_war/commons/fonts.dart';
import 'package:aircraft_war/components/main_game.dart';
import 'package:aircraft_war/foundation/sounds.dart';
import 'package:aircraft_war/generated/assets.dart';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:flame/input.dart';
import 'package:flutter/cupertino.dart';

final _textPaint = TextPaint(
  style: const TextStyle(
    color: CupertinoColors.label,
    fontSize: 20,
    fontWeight: FontWeight.w500,
    fontFamily: handwritten,
  ),
);
final _buttonSize = Vector2.all(30);

/// Created by changlei on 2022/5/18.
///
/// 暂停按钮
class PauseButton extends GameStatePositionComponent {
  int _value = 0;
  Vector2? _parentSize;

  /// 设置值
  set value(int value) {
    if (_value == value) {
      return;
    }
    _value = value;
    _score.text = value.toString();
    if (_parentSize != null) {
      layout(_parentSize!);
    }
  }

  late final _pauseButton = SpriteButtonComponent(
    button: Sprite(Flame.images.fromCache(Images.resume_nor)),
    buttonDown: Sprite(Flame.images.fromCache(Images.resume_pressed)),
    size: _buttonSize,
    onPressed: () {
      Sounds.button();
      final state = gameRef.stateCubit.state;
      if (state != GameState.running && state != GameState.pausing) {
        return;
      }
      if (state == GameState.running) {
        gameRef.stateCubit.pause();
      } else {
        gameRef.stateCubit.run();
      }
    },
  );
  late final _score = _ScoreValue(
    text: _value.toString(),
    textRenderer: _textPaint,
    position: Vector2(_pauseButton.width + 10, 0),
  );

  @override
  Future<void> onLoad() async {
    await add(_pauseButton);
    await add(_score);
    onNewState(gameRef.stateCubit.state);
  }

  @override
  void onNewState(GameState state) {
    switch (state) {
      case GameState.idle:
      case GameState.pending:
      case GameState.running:
      case GameState.stopped:
        _pauseButton.button = Sprite(Flame.images.fromCache(Images.pause_nor));
        _pauseButton.buttonDown = Sprite(Flame.images.fromCache(Images.pause_pressed));
        break;
      case GameState.pausing:
        _pauseButton.button = Sprite(Flame.images.fromCache(Images.resume_nor));
        _pauseButton.buttonDown = Sprite(Flame.images.fromCache(Images.resume_pressed));
        break;
    }
    if (isMounted) {
      _pauseButton.onMount();
    }
  }

  /// 布局
  void layout(Vector2 parentSize) {
    _parentSize = parentSize;
    size = Vector2(_pauseButton.width + _textPaint.measureTextWidth(_value.toString()) + 10, _pauseButton.height);
    position = Vector2.all(20);
  }
}

class _ScoreValue extends TextComponent with BlocListenable<GameScoreCubit, int>, HasGameRef<MainGame> {
  _ScoreValue({
    super.text,
    super.textRenderer,
    super.position,
  });

  @override
  void onNewState(int state) {
    (parent as PauseButton).value = state;
  }

  @override
  Future<void> onLoad() async {
    onNewState(gameRef.scoreCubit.state);
  }

  @override
  void onMount() {
    super.onMount();
    onNewState(gameRef.scoreCubit.state);
  }
}
